/**
 * @Author: shyn
 * @Description:
 * @File: tmpl //文件名称
 * @Version: 1.0.0
 * @Date: 2022/2/21 9:03  //时间
 * @Software : GoLand //开发软件【可选】
 */
package tmpl

import (
	"encoding/json"
	"fmt"
	"html/template"
	"log"
	"net/http"
	"server/robot"
)

func sayHello(w http.ResponseWriter, c *http.Request) {
	output := "sayHello"
	_, err := fmt.Fprintln(w, output)

	if err != nil {
		fmt.Println("output error", err)
		return
	}
}
func sayHelloJson(w http.ResponseWriter, c *http.Request) {
	user := new(robot.User) //
	user.Name = "shyn"
	user.Age = 30
	user.Say()
	u, _ := json.Marshal(user)
	_, err := fmt.Fprintln(w, string(u))

	if err != nil {
		fmt.Println("output error", err)
		return
	}
}
func sayHelloTmpl(w http.ResponseWriter, c *http.Request) {

	tmpl, err := template.ParseFiles("./tool/tmpl/hello.tmpl")
	if err != nil {
		fmt.Println("tmpl err", err)
		return
	}

	//user := robot.User{Name: "shyn", Age: 30}
	user := new(robot.User) //
	user.Name = "shyn"
	user.Age = 30
	terr := tmpl.Execute(w, user)

	if err != nil {
		fmt.Println("tmp exec failed", terr)
	}
}

func Start1() {
	http.HandleFunc("/hello", sayHello)
	err := http.ListenAndServe(":9001", nil)
	if err != nil {
		log.Fatal(err)
	}
}

func Start2() {
	http.HandleFunc("/hello", sayHelloTmpl)
	err := http.ListenAndServe(":9001", nil)
	if err != nil {
		log.Fatal(err)
	}
}
func Start3() {
	http.HandleFunc("/hello", sayHelloJson)
	err := http.ListenAndServe(":9001", nil)
	if err != nil {
		log.Fatal(err)
	}
}
